package com.gtw.modules.api.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.annotation.AuthIgnoreApi;
import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.PayStatus;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.dto.TbDiscussDto;
import com.gtw.modules.api.service.*;
import com.gtw.modules.entity.*;
import com.gtw.utils.EntityDtoConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * 用户评论
 * Created by wuye on 2018/2/1.
 */
@RestController
@RequestMapping("/api/discuss")
public class TbDiscussController {

    @Autowired
    private TbDiscussService tbDiscussService;

    @Autowired
    private TbLikeService tbLikeService;

    @Autowired
    private TbTokenService tbTokenService;
    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TbOrderService tbOrderService;


    /**
     * 获取商品评论列表
     *
     * @return
     */
    @AuthIgnoreApi
    @RequestMapping(value = "/comment", method = RequestMethod.GET)
    public R discuss(@RequestParam Map<String, Object> params) throws InvocationTargetException, IllegalAccessException {
        TbUser tbUser = new TbUser();
        if (params.get("token")!=null) {
            TbToken tbToken = tbTokenService.queryByToken(params.get("token").toString());
            if (tbToken!=null) {
                tbUser = tbUserService.foundById(tbToken.getUserId());
                params.put("uid", tbUser.getUuid());
            }
        }
        //查询列表数据
        Query query = new Query(params);
        Page<TbDiscuss> pageUtil = new Page<TbDiscuss>(query.getPage(), query.getLimit());
        Page<TbDiscuss> page = tbDiscussService.queryPageList(pageUtil, query);

        Page<TbDiscussDto> pageUtil2 = new Page<TbDiscussDto>(query.getPage(), query.getLimit());
        List<TbDiscussDto> tbDiscussList = new ArrayList<>();
        EntityDtoConverter edc = new EntityDtoConverter();
        for (int i = 0; i < page.getRecords().size(); i++) {
            TbDiscussDto tbDiscussDto = new TbDiscussDto();
            tbDiscussDto = (TbDiscussDto) edc.entityConvertDto(page.getRecords().get(i), tbDiscussDto);
            tbDiscussDto.setTbUser(tbUserService.queryByUid(page.getRecords().get(i).getUid()));
            if (params.get("token").equals(null)) {
                tbDiscussDto.setIslike(false);
            } else {
                TbLike tbLike = tbLikeService.selectByUid(tbUser.getUuid(), tbDiscussDto.getId());
                List<TbLike> tbLikeList = tbLikeService.selectByDiscussid(tbDiscussDto.getId());
                tbDiscussDto.setIslike(tbLike == null ? false : true);
                tbDiscussDto.setLikenum(tbDiscussList.size());
            }
            tbDiscussList.add(tbDiscussDto);
        }
        pageUtil2.setCondition(page.getCondition());
        pageUtil2.setRecords(tbDiscussList);


        return R.ok().put("data", pageUtil2);

    }

    /**
     * 提交评价
     *
     * @return
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public R submit(String billno, Integer commodityid, String pic, String content, @LoginUserApi TbUser tbUser) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", 1);
        params.put("limit", Integer.MAX_VALUE);
        params.put("uid", tbUser.getUuid());
        params.put("commodityid", commodityid);
        //查询列表数据
        Query query = new Query(params);
        Page<TbDiscuss> pageUtil = new Page<TbDiscuss>(query.getPage(), query.getLimit());
        Page<TbDiscuss> page = tbDiscussService.queryPageList(pageUtil, query);
        if (page.getRecords().size() > 0) {
            return R.error("您已经评论过了！");
        }
        TbOrder tbOrder = tbOrderService.queryBybillno(billno);
        if (tbOrder.getOrderstatus().equals(PayStatus.uncommented.getName())) {
            tbOrder.setOrderstatus(PayStatus.success.getName());
            tbOrderService.update(tbOrder);
        }
        TbDiscuss tbDiscuss = new TbDiscuss();
        tbDiscuss.setCommodityid(commodityid);
        tbDiscuss.setContent(content);
        tbDiscuss.setPic(pic);
        tbDiscuss.setUid(tbUser.getUuid());
        tbDiscuss.setCreatedate(new Date());
        tbDiscussService.insert(tbDiscuss);
        return R.ok();

    }

    /**
     * 点赞
     *
     * @return
     */
    @RequestMapping(value = "/like", method = RequestMethod.POST)
    public R like(Integer discussid, @LoginUserApi TbUser tbUser) {
        TbLike tbLike = tbLikeService.selectByUid(tbUser.getUuid(), discussid);
        if (tbLike == null) {
            tbLike = new TbLike();
            tbLike.setCreatedate(new Date());
            tbLike.setDiscussid(discussid);
            tbLike.setIslike(true);
            tbLike.setUid(tbUser.getUuid());
            tbLikeService.insert(tbLike);
            return R.ok().put("data", tbLike);
        } else {
            tbLikeService.deleteByPrimaryKey(tbLike.getId());
            return R.ok();
        }
    }


}
